package cn.edu.lsu.integratedsurvey.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 跨域配置
 *
 * @author Jasmine-drs
 * @date 2022-10-24
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
//        设置是否允许携带cookie
        config.setAllowCredentials(true);
        // 设置访问源地址
//        config.addAllowedOrigin("*");
        config.addAllowedOrigin("http://localhost:3000");
        config.addAllowedOrigin("http://1.12.249.127:8080");
        config.addAllowedOrigin("http://localhost:8080");
//        config.addAllowedOrigin("http://127.0.0.1:8083");
//        config.addAllowedOrigin("http://localhost:8083");
//        config.addAllowedOrigin("https://drs.mynatapp.cc");
        // 设置访问源请求头
        config.addAllowedHeader("*");
        // 设置访问源请求方法
        config.addAllowedMethod("*");
        // 有效期 1800秒
        config.setMaxAge(1800L);
        // 添加映射路径，拦截一切请求
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        // 返回新的CorsFilter
        return new CorsFilter(source);
    }
}

